//
//  wslAnalyzer.m
//  歌词解析1
//
//  Created by 王双龙 on 2017/6/5.
//  Copyright © 2017年 https://github.com/wslcmk All rights reserved.//

#import "wslAnalyzer.h"
#import "wslLrcEach.h"
#import "JHEncrypt.h"

@implementation wslAnalyzer

-(NSMutableArray *)lrcArray
{
    if (_lrcArray == nil) {
        _lrcArray = [[NSMutableArray alloc] init];
    }return _lrcArray;
}

-(NSMutableArray *)analyzerLrcByPath:(NSString *)path
{
    
    [self  analyzerLrc:[NSString   stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] SeparatedByString:@"["];
    
    return self.lrcArray;
}

-(NSMutableArray *)analyzerLrcBylrcString:(NSString *)string{
    
    if (!self.isWord)
    {
        [self  analyzerLrc:string SeparatedByString:@"\n"];
    }
    else
    {
        [self  analyzerLrc:string SeparatedByString:@"\n"];
    }
    
    return self.lrcArray;
}

//根据换行符\n分割字符串，获得包含每一句歌词的数组
-(void)analyzerLrc:(NSString *)lrcConnect SeparatedByString:(NSString *)str
{
    
    NSArray *  lrcConnectArray = [lrcConnect   componentsSeparatedByString:str];
    
    NSMutableArray    *  lrcConnectArray1 =[[NSMutableArray  alloc] initWithArray: lrcConnectArray ];
    
    for (NSUInteger i = 0;  i < [lrcConnectArray1  count]  ;i++ ) {
        //删除空行
        if ([lrcConnectArray1[i]   length] == 0 ) {
            [lrcConnectArray1  removeObjectAtIndex:i];
            i--;
        }else{//获取解密的字符串
            lrcConnectArray1[i] = [JHEncrypt getMstr:lrcConnectArray1[i]];
        }
    }
    
    if (!self.isWord)
    {
        [self analyzerEachLrc:lrcConnectArray1];
    }
    else
    {
        [self analyzerEachLrcWord:lrcConnectArray1];
    }
}
//删除没有用的字符
-(NSMutableArray *)deleteNoUseInfo:(NSMutableArray *)lrcmArray
{
    for (NSUInteger i = 0; i < [lrcmArray count] ; i++)
    {
        unichar  ch = [lrcmArray[i] characterAtIndex:1];
        if(!isnumber(ch)){
            [lrcmArray removeObjectAtIndex:i];
            i--;
        }
    }
    return lrcmArray;
}
-  (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

//解析每一行课文
-(void)analyzerEachLrc:(NSMutableArray *)lrcConnectArray
{
    for (NSUInteger i = 0;  i < [lrcConnectArray  count] ;  i++) {
        if(![lrcConnectArray[i] containsString:@"[" ]
           || ![lrcConnectArray[i] containsString:@"]" ]){
            continue;
        }
        
        NSArray * eachLrcArray = [lrcConnectArray[i]   componentsSeparatedByString:@"]"];
        //拼接lrc
        NSString * lrc = @"";
        for(NSUInteger j = 0;  j < [eachLrcArray  count] ;  j++){
            if(j==0){
                continue;
            }
            if (j!= [eachLrcArray count] -1){
                 lrc = [NSString stringWithFormat:@"%@%@]", lrc, eachLrcArray[j] ];
            }else{
                 lrc = [NSString stringWithFormat:@"%@%@", lrc, eachLrcArray[j] ];
            }
           
        }
        
        if([self isBlankString:lrc]){
            continue;
        }
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df  setDateFormat:@"mm:ss.SS"];
        NSDate * date1 = [df  dateFromString:[eachLrcArray[0] stringByReplacingOccurrencesOfString:@"[" withString:@""]];
        NSDate *date2 = [df dateFromString:@"00:00.00"];
        NSTimeInterval  interval1 = [date1  timeIntervalSince1970];
        NSTimeInterval  interval2 = [date2  timeIntervalSince1970];
        interval1 -= interval2;
        if (interval1 < 0) {
            interval1 *= -1;
        }
        
        //去掉换行
        if (lrc.length >0){
            NSString * str = [lrc substringWithRange:NSMakeRange(lrc.length - 1, 1)];
            if ([str isEqualToString:@"\n"])
            {
                lrc = [lrc substringToIndex:lrc.length - 1];
            }
        }
        //去掉前后空格
        lrc=[lrc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if([self isBlankString:lrc]){
            continue;
        }
        
        wslLrcEach   * eachLrc = [[wslLrcEach alloc] init];
        eachLrc.lrc_en =lrc;
        if((i+1) < [lrcConnectArray  count]){
            i++;
            eachLrc.lrc_ch =lrcConnectArray[i];
        }else{
            eachLrc.lrc_ch =@"";
        }
        eachLrc.lrc = [NSString stringWithFormat:@"%@\n%@", eachLrc.lrc_en, eachLrc.lrc_ch ];
        eachLrc.time =  interval1;
        [self.lrcArray addObject:eachLrc];
    }
}

//解析单词
-(void)analyzerEachLrcWord:(NSMutableArray *)lrcConnectArray
{
    for (NSUInteger i = 0;  i < [lrcConnectArray  count] ;  i++) {
        
        NSString * sub = [lrcConnectArray[i] substringToIndex:1];
        if ([sub isEqualToString:@"["] &&
            ((NSString *)(lrcConnectArray[i])).length >10)
        {
            NSString * timeStr = [lrcConnectArray[i] substringToIndex:10];
            timeStr = [timeStr stringByReplacingOccurrencesOfString:@"[" withString:@""];
            timeStr = [timeStr stringByReplacingOccurrencesOfString:@"]" withString:@""];
            NSString * wordStr = [lrcConnectArray[i] substringFromIndex:10];
            
            NSDateFormatter *df = [[NSDateFormatter alloc] init];
            [df  setDateFormat:@"mm:ss.SS"];
            
            NSDate * date1 = [df  dateFromString:timeStr];
            NSDate *date2 = [df dateFromString:@"00:00.00"];
            NSTimeInterval  interval1 = [date1  timeIntervalSince1970];
            NSTimeInterval  interval2 = [date2  timeIntervalSince1970];
            interval1 -= interval2;
            if (interval1 < 0) {
                interval1 *= -1;
            }
            
            //去掉前后空格
            wordStr=[wordStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            //如果时间点对应的歌词为空就不加入歌词数组
            if (wordStr.length == 0 || [wordStr isEqualToString:@"\r"] || [wordStr isEqualToString:@"\n"]) {
                continue;
            }
            
            wslLrcEach   * eachLrc = [[wslLrcEach alloc] init];
            NSArray *arrayLrc ;
            bool isAppend=false;
            if ([wordStr containsString:@"]"])
            {
                arrayLrc = [wordStr componentsSeparatedByString:@"]"];
                isAppend =true;
            }
            else if ([wordStr containsString:@"#"])
            {
                arrayLrc = [wordStr componentsSeparatedByString:@"#"];
            }
            if ([arrayLrc count] ==1){
                eachLrc.lrc_en = arrayLrc[0];
                eachLrc.lrc_ch = @"";
            }else if ([arrayLrc count] >=2){
                eachLrc.lrc_en = arrayLrc[0];
                eachLrc.lrc_ch = [arrayLrc[1] stringByReplacingOccurrencesOfString:@"&" withString:@""];
            }else{
                eachLrc.lrc_en = @"";
                eachLrc.lrc_ch = @"";
            }
            if (isAppend){
                eachLrc.lrc_en =[NSString stringWithFormat:@"%@]", eachLrc.lrc_en ];
            }
            eachLrc.lrc = [NSString stringWithFormat:@"%@\n%@", eachLrc.lrc_en, eachLrc.lrc_ch ];
            eachLrc.time =  interval1;
            if ([arrayLrc count] >0){
                [self.lrcArray addObject:eachLrc];
            }
        }
        
    }
}





@end
